const query = require('../config/db.config.js')
const {
  nowTime
} = require('../utils/formattime')

const userMod = {
  validateAccMOd: async (data) => {
    const { acc } = data
    let sql = `
      SELECT COUNT(*) total FROM admin where acc="${acc}"`
    const res = await query(sql)
    return res[0].total
  },
  validatePWdMOd: async (data) => {
    const { acc, pwd } = data
    let sql = ` SELECT pwd FROM admin where acc="${acc}"`
    const res = await query(sql)
    if (res[0].pwd !== pwd) {
      return 0
    }
    return 1
  },
  selectWebuser: async (data) => {
    const { acc } = data
    let sql = ` SELECT * FROM admin where acc="${acc}"`
    const res = await query(sql)
    return res
  },
  userEditInfoMod: async (payload) => {
    let sql1 = `UPDATE appeal set uname = "${payload.uname}" WHERE uID = "${payload.uID}"`
    await query(sql1)
    let sql2 =
      `UPDATE user 
    set 
    uname = "${payload.uname}", 
    phone = "${payload.phone}",
    home = "${payload.home}",
    identity = "${payload.identity}",
    avatarUrl = "${payload.avatarUrl}" 
    WHERE uID = "${payload.uID}"`
    await query(sql2)
  },
  selectwxPerson: async (id) => {
    let sql2 = `SELECT uID,uname,phone,home,identity,avatarUrl FROM user WHERE uID = "${id}";`
    const res = await query(sql2)
    return res
  },
  createwxUser: async (payload) => {
    let createUserSql =
      `INSERT INTO user 
      (uID,uname,phone,home,identity,avatarUrl) 
    VALUES 
      ("${payload.uID}", "${payload.uname}", "${payload.phone}","${payload.home}","${payload.identity}","${payload.avatarUrl}" );`
    await query(createUserSql)
  },
  isRegister: async (id) => {
    let sql = `SELECT COUNT(*) has FROM user WHERE uID = "${id}";`
    const res = await query(sql)
    return res[0].has
  },
  createAppeal: async (payload) => {
    const appeal_imgs = payload.appeal_imgs.join(',')
    let createUserSql =
      `INSERT INTO appeal 
      (uID,title,content,appeal_imgs,uname) 
    VALUES 
      ("${payload.uID}", "${payload.title}", "${payload.content}","${appeal_imgs}","${payload.uname}")`
    await query(createUserSql)
  },
  webAppeallistMod: async (data) => {
    const { uname, admName, status, title, limit, page } = data
    let pageCurr = (page - 1) * limit
    let occ = ''
    let sql =
      `SELECT  
      *
    FROM 
      appeal ad
    where 
     (ad.uname LIKE '%${uname || occ}%' )
     AND  (ad.admName LIKE '%${admName || occ}%' )
     AND  (ad.title LIKE '%${title || occ}%' )
     AND   (ad.status  LIKE '%${status || occ}%')
     order by ctime desc
       LIMIT ${pageCurr},${limit}
    `
    console.log(sql);
    let sql2 = `SELECT COUNT(*) total FROM appeal ;`
    const total = await query(sql2)
    let res = await query(sql)
    res.forEach(i => {
      if (i.appeal_imgs) {
        i.appeal_imgs = i.appeal_imgs.split(',')
      } else {
        i.appeal_imgs = []
      }
    })
    return {
      total: total[0].total,
      list: res
    }
  },
  editAnnouceMod: async (data) => {
    let {
      admID,
      content
    } = data
    let sql =
      `UPDATE announcement 
      SET
      content = "${content}"
      where admID = ${admID} 
    `
    await query(sql)
  },
  userEvoMod: async (data) => {
    let {
      id,
      evaluation
    } = data
    let sql =
      `UPDATE appeal 
      SET
      evaluation = "${evaluation}"
      where id = "${id} "
    `
    await query(sql)
  },
  userAppealMod: async (data) => {
    let {
      uID
    } = data
    let sql =
      `SELECT  
      *
    FROM 
      appeal
    WHERE
     uID = "${uID}"
     order by ctime desc
     
    `
    const res = await query(sql)
    // console.log(res);
    return res
  },
  userlistMod: async (data) => {
    const { uname, phone, home, identity, color, fever, home_fever, limit, page } = data
    let occ = ''
    let pageCurr = (page - 1) * limit
    let sql =
      `SELECT  
      *
    FROM 
      user
		WHERE
			uname  LIKE '%${uname || occ}%'
			AND phone  LIKE '%${phone || occ}%'
			AND home  LIKE '%${home || occ}%'
			AND identity  LIKE '%${identity || occ}%'
			AND color  LIKE '%${color || occ}%'
			AND fever  LIKE '%${fever || occ}%'
			AND home_fever  LIKE '%${home_fever || occ}%'
      ORDER BY heal_time DESC
      LIMIT ${pageCurr},${limit}
    `
    let sql2 = `SELECT COUNT(*) total FROM user ;`
    const total = await query(sql2)
    const res = await query(sql)
    return {
      total: total[0].total,
      list: res
    }
  },
  userHealMod: async (data) => {
    const {
      fever,
      homefever,
      color,
      uID
    } = data
    const time = nowTime()
    let sql =
      `UPDATE user 
     SET
       fever = "${fever}",
       home_fever = "${homefever}",
       color = "${color}",
       heal_time = "${time}"
     WHERE 
       uID = "${uID}"`
    await query(sql)
    const res = await query(sql)


    return res
  },
  deleteAppealwebMod: async (data) => {
    const { id } = data
    let sql = `DELETE FROM appeal  where  id = "${id}"`
    await query(sql)
  },
  editAppealwebMod: async (data) => {
    let {
      id,
      status,
      admID,
      admName,
      reason
    } = data
    let occ = ''
    let sql =
      `UPDATE appeal 
      SET
      status = "${status}",
      admID = "${admID}",
      admName = "${admName}",
      reason = "${reason || occ}"
      where id = "${id}"
    `
    await query(sql)
  },
}

module.exports = userMod